package com.example.test_app.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.ManagerFragment;
import com.example.test_app.R;
import com.example.test_app.view.MyFragmentPagerAdapter;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author a
 */
public class ViewPageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<List<Fragment>> data;
    private FragmentManager fragmentManager;
    private ManagerFragment managerFragment;
    private Queue<Integer> idQueue = new LinkedList<>();
    private int maxId = 0;


    public int getVpId() {
        if (idQueue.isEmpty() || idQueue.size() < 5) {
            maxId++;
            return maxId;
        } else {
            return idQueue.poll();
        }
    }

    public void removeId(int id) {
        idQueue.remove(id);
    }

    public void addId2Queue(int i) {
        idQueue.add(i);
    }

    public ViewPageAdapter(Context context, List<List<Fragment>> data, FragmentManager fragmentManager) {
        this.context = context;
        this.data = data;
        this.fragmentManager = fragmentManager;
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        managerFragment = new ManagerFragment(recyclerView);
        fragmentManager.beginTransaction().add(managerFragment, recyclerView.toString())
                .commitAllowingStateLoss();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new VpHolder(LayoutInflater.from(context).inflate(R.layout.item_vp, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int p0) {
        Log.d("ViewPageAdapter", "onBindViewHolder: " + p0);
        if (holder instanceof VpHolder) {
            ViewPager viewPager = ((VpHolder) holder).viewPager;
            int id = getVpId();
            if (holder.itemView.getId() == id) {
                id = getVpId();
            }
            Log.d("ViewPageAdapter", "onBindViewHolder: viewPagerID" + id);
            viewPager.setId(id);

            final int p = p0;

            viewPager.setAdapter(new MyFragmentPagerAdapter(managerFragment.getChildFragmentManager(), data.get(p)));


        }

    }


    @Override
    public void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        if (holder instanceof VpHolder) {
            ViewPager viewPager = ((VpHolder) holder).viewPager;
            addId2Queue(viewPager.getId());
        }
    }

    @Override
    public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        if (holder instanceof VpHolder) {
            ViewPager viewPager = ((VpHolder) holder).viewPager;
            removeId(viewPager.getId());
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    private class VpHolder extends RecyclerView.ViewHolder {
        public ViewPager viewPager;

        public VpHolder(@NonNull View itemView) {
            super(itemView);
            viewPager = itemView.findViewById(R.id.vp);
        }
    }
}
